var phoneNumber1 = "";
var phoneNumber2 = "";

function displayMaintainRecord() {
    updateMaintainData();

    //设置侧边栏点击显示效果
    document.getElementById("maintain-record").style.background = 'rgba(0, 0, 40, 0.83)';
    document.getElementById("maintain-record").style.color = 'white';

    document.getElementById("complaint-record").style.background = 'rgb(132, 133, 154)';
    document.getElementById("complaint-record").style.color = '#140000';

    document.getElementById("service-online").style.background = 'rgb(132, 133, 154)';
    document.getElementById("service-online").style.color = '#140000';

    var urlParams = new URLSearchParams(window.location.search);

    //显示记录并隐藏另一记录
    document.getElementById("complaint-record-content").style.display = "none";
    document.getElementById("maintain-record-content").style.display = "block";
}

function displayComplaintRecord() {
    updateComplaintData()

    //设置侧边栏点击显示效果
    document.getElementById("maintain-record").style.background = 'rgb(132, 133, 154)';
    document.getElementById("maintain-record").style.color = '#140000';

    document.getElementById("complaint-record").style.background = 'rgba(0, 0, 40, 0.83)';
    document.getElementById("complaint-record").style.color = 'white';

    document.getElementById("service-online").style.background = 'rgb(132, 133, 154)';
    document.getElementById("service-online").style.color = '#140000';

    var urlParams = new URLSearchParams(window.location.search);

    //显示记录并隐藏另一记录
    document.getElementById("maintain-record-content").style.display = "none";
    document.getElementById("complaint-record-content").style.display = "block";
}

function goToHtmlFileServiceOnline() {
    window.open('ServiceOnline.html', '_blank');
}

function updateMaintainData() {
    fetch("/maintain5")
        .then(response => response.json())
        .then(data => {
            document.getElementById("maintain_form_col2_row1_staff").innerHTML = data.phoneNumber;
            document.getElementById("maintain_form_col2_row2_staff").innerHTML = data.issueType;
            document.getElementById("maintain_form_col2_row3_staff").innerHTML = data.appointmentDate;
            document.getElementById("maintain_form_col2_row4_staff").innerHTML = data.issueDetails;
            phoneNumber1 = data.phoneNumber;
        })
        .catch(error => {
            console.error('Error fetching data:', error);
        });
}

function updateSignedStatus() {
    var repairRecord = {
        signedStatus: 1,
        phoneNumber: phoneNumber1
    }

    var xhr = new XMLHttpRequest();
    xhr.open("POST", "/maintain2", true);
    xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
    xhr.onreadystatechange = function () {
        if (xhr.readyState === XMLHttpRequest.DONE) {
            if (xhr.status === 200) {
                alert(xhr.responseText);
                window.location.href = "homeServiceStaff.html";
            } else {
                alert("Error: " + xhr.status);
            }
        }
    };
    xhr.send(JSON.stringify(repairRecord));
}

function updateComplaintData() {
    fetch("/complaint6")
        .then(response => response.json())
        .then(data => {
            document.getElementById("complaint_form_col2_row1_staff").innerHTML = data.phoneNumber;
            document.getElementById("complaint_form_col2_row2_staff").innerHTML = data.issueType;
            document.getElementById("complaint_form_col2_row3_staff").innerHTML = data.issueDetails;
            phoneNumber2 = data.phoneNumber;
        })
        .catch(error => {
            console.error('Error fetching data:', error);
        });
}

function updateSignedStatus1() {
    var repairRecord = {
        signedStatus: 1,
        phoneNumber: phoneNumber2
    }

    var xhr1 = new XMLHttpRequest();
    xhr1.open("POST", "/complaint4", true);
    xhr1.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
    xhr1.onreadystatechange = function () {
        if (xhr1.readyState === XMLHttpRequest.DONE) {
            if (xhr1.status === 200) {
                alert(xhr1.responseText);
                window.location.href = "homeServiceStaff.html";
            } else {
                alert("Error: " + xhr1.status);
            }
        }
    };
    xhr1.send(JSON.stringify(repairRecord));
}